package com.psg.controller;


import com.github.pagehelper.PageInfo;
import com.psg.pojo.Look;
import com.psg.pojo.User;
import com.psg.service.ProduceService;
import com.psg.service.impl.UserServiceImpl;
import com.psg.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserServiceImpl userService;
    @Autowired
    private ProduceService produceService;

    /**
     * 跳转登录页面
     * @return
     */
    @RequestMapping("/toLogin")
    private String toLogin(){
        return "login";
    }

    /**
     * 用户登录
     * @param code
     * @param user
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("/login")
    @ResponseBody
    public Result login(@RequestParam("code123")String code, User user, HttpServletRequest request, Model model){
        String  codes2 =(String) request.getSession().getAttribute("codes2");
        System.out.println("codes2："+codes2);
        System.out.println("codes3："+code);
        if (!codes2.equals(code)){
            return Result.error("验证码错误");
        }
        User login = userService.login(user.getuName(), user.getPwd());
        System.out.println("传入的"+user);
        System.out.println(login);


        if (login!=null){
            System.out.println("登录的用户："+login);
            request.getSession().setAttribute("login",login);
            return Result.success("登录成功");
        }else {
            return Result.error("用户名或密码错误，请重新输入");
        }
    }

    /**
     * 用户注销
     * @param request
     * @return
     */
    @RequestMapping("/loginOut")
    public String  loginOut(HttpServletRequest request){
        HttpSession session = request.getSession();
        session.removeAttribute("login");
        return "redirect:/index";
    }

    /**
     * 跳转用户注册页面
     * @return
     */
    @GetMapping("/toRegister")
    private String toRegister(){
        return "register";
    }


    /**
     * 用户注册
     * @param user
     * @return
     * @throws IOException
     */
    @PostMapping("/register")
    public String register(User user) throws IOException {
        System.out.println("要注册的"+user);
        userService.register(user.getuName(), user.getPwd(), user.getEmail());
        //response.sendRedirect("/login");
        return "login";
    }

    /**
     * 跳转忘记密码页面
     * @return
     */
    @RequestMapping("/toForgetPwd")
    private String toForgetPwd(){
        return "forgetPwd";
    }

    @RequestMapping("/record")
    public ModelAndView customer(ModelAndView mav){
        PageInfo<Look> pageInfo = produceService.recordPage(1, 5);
        mav.addObject("pages",pageInfo);
        mav.setViewName("customer");
        return mav;
    }


}
